<template>
  <a-radio-group v-model="value"  :direction="props.direction" :type="props.type" :disabled="props.disabled" @change="handleChangeEvent($event)">
    <template v-for="(item, index) in props.data" key="index">
      <a-radio :value="item[props.valueKey]">{{ item[props.titleKey] }}</a-radio>
    </template>
  </a-radio-group>
</template>

<script setup>
import { ref, watch } from 'vue'
const emit = defineEmits(['update:modelValue', 'change'])
const value = ref()

const props = defineProps({
  modelValue: { type: [String, Number] },
  type: { type: String, default: 'radio' },
  data: { type: Array, default: [] },
  disabled: { type: Boolean, default: false },
  direction: { type: String, default: 'horizontal' },
  valueKey:{ type: [String, Number], default: 'value' },
  titleKey:{ type: String, default: 'title' }
})
watch(
  () => props.modelValue,
  (vl) => {
      value.value = vl
  },
  { immediate: true }
)
watch(
  () => value.value,
  (v) => {
    emit('update:modelValue', value.value)
  }
)

const handleChangeEvent = async (value) => {
  emit('update:modelValue', value)
  emit('change', value)
}
</script>
